package com.ruoyi.api.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.business.domain.dto.api.user.UpdateUserDTO;
import com.ruoyi.business.domain.koc.TbProductExchangeCoupon;
import com.ruoyi.business.domain.koc.TbUserKocPointsLog;
import com.ruoyi.business.domain.koc.TbUserKocProductLog;
import com.ruoyi.business.domain.koc.TbUserKocTaskInfo;
import com.ruoyi.business.domain.vo.TbUserKocPointsLogStatisticVO;
import com.ruoyi.business.domain.vo.TbUserKocTaskInfoAppVO;
import com.ruoyi.business.domain.vo.TbUserKocTaskVO;
import com.ruoyi.business.domain.vo.business.TbUserDetailVO;
import com.ruoyi.business.service.koc.*;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.business.CouponStatusEnum;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序用户管理Controller
 */
@Validated
@Api(value = "小程序用户管理控制器", tags = {"小程序用户管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/miniapp/user")
public class ApiUserController extends AbstractApiController {

    private final IUserService iUserService;

    @Resource
    private ITbUserService userService;

    @Resource
    private ITbKocTaskTypeService kocTaskTypeService;

    @Resource
    private ITbUserKocTaskInfoService tbUserKocTaskInfoService;

    @Resource
    private ITbUserKocPointsLogService tbUserKocPointsLogService;

    @Resource
    private ITbUserKocProductLogService tbUserKocProductLogService;

    @Resource
    private ITbProductExchangeCouponService tbProductExchangeCouponService;
    /**
     * 获取用户管理详细信息
     */
    @ApiOperation("获取用户管理详细信息")
    @GetMapping("/info")
    public R<User> getInfo() {
        return R.ok(iUserService.getById(getUserId(), true));
    }


    /**
     * 查询用户详细信息
     */
    @ApiOperation("查询用户详细信息用于群裂变")
    @GetMapping("/user-info-detail")
    public R<TbUserDetailVO> getUserInfoDetail() {
        return R.ok(userService.getUserDetailById(getUserId()));
    }

    /**
     * 修改用户基本信息 头像等
     */
    @ApiOperation("完善用户基本信息头像等")
    @PutMapping("/update")
    public R<Integer> updateUserInfo(@Validated @RequestBody UpdateUserDTO dto) {
        dto.setUserId(getUserId());
        return R.ok(userService.updateUserInfo(dto));
    }

    /**
     * 查询用户可做与必做任务类型与任务信息
     */
    @ApiOperation("查询koc用户当前koc等级可做的任务类型与任务")
    @GetMapping("/query-user-koc-task")
    public R<TbUserKocTaskVO> queryUserKocTask() {
        return R.ok(kocTaskTypeService.queryByUserKocLevel(getUserId()));
    }

    /**
     * 查询我的任务记录
     */
    @ApiOperation("查询我的koc任务提交记录")
    @GetMapping("/query-my-task-info")
    public TableDataInfo<TbUserKocTaskInfo> queryMyKocTaskInfo(
        @ApiParam("koc期次") @RequestParam(required = false) String kocDate,
        @ApiParam("任务类型id") @RequestParam(required = false) Long kocTaskTypeId,
        @ApiParam("任务id") @RequestParam(required = false) Long kocTaskId,
        PageQuery pageQuery) {
        return tbUserKocTaskInfoService.selectMyTaskInfo(getUserId(), kocTaskTypeId, kocTaskId, kocDate, pageQuery);
    }


    @ApiOperation("查询我的koc任务详情")
    @GetMapping("/query-my-task-info-detail")
    public R<TbUserKocTaskInfoAppVO> queryMyKocTaskInfoDetail(
        @ApiParam("审核任务主键id") @RequestParam(required = true) Long kocTaskInfoId) {
        return R.ok(tbUserKocTaskInfoService.selectMyTaskInfoDetail(getUserId(), kocTaskInfoId));
    }

    /**
     * 汇总
     */

    @ApiOperation("查询我的积分流水汇总")
    @GetMapping("/query-my-point-log-statistic")
    public TableDataInfo<TbUserKocPointsLogStatisticVO> queryMyPointLogStatistic(
        @ApiParam("koc期次") @RequestParam(required = false) String kocDate,
        PageQuery pageQuery) {
        return tbUserKocPointsLogService.queryMyPointLogsStatisticPage(getUserId(), kocDate, pageQuery);
    }


    /**
     * 查询我的积分流水
     */
    @ApiOperation("查询我的积分流水")
    @GetMapping("/query-my-point-log")
    public TableDataInfo<TbUserKocPointsLog> queryMyPointLog(
        @ApiParam("koc期次") @RequestParam(required = false) String kocDate,
        @ApiParam("审核任务id") @RequestParam(required = false) Long checkBusinessId,
        @ApiParam("任务id") @RequestParam(required = false) Long kocTaskId,
        @ApiParam("增减类型 1 增加 2 减少") @RequestParam(required = false) Integer type,
        @ApiParam("来源类型 1:koc任务审核 2:管理员操作积分") @RequestParam(required = false) Integer sourceType,
        PageQuery pageQuery) {
        return tbUserKocPointsLogService.queryMyPointLogsPage(getUserId(), checkBusinessId, kocDate, kocTaskId, type, sourceType, pageQuery);
    }

    /**
     * 查询我的产品流水
     */
    @ApiOperation("查询我的产品流水")
    @GetMapping("/query-my-product-log")
    public TableDataInfo<TbUserKocProductLog> queryMyProductLog(
        @ApiParam("koc期次") @RequestParam(required = false) String kocDate,
        @ApiParam("审核任务id") @RequestParam(required = false) Long checkBusinessId,
        PageQuery pageQuery) {
        return tbUserKocProductLogService.queryMyProductLogsPage(getUserId(), checkBusinessId, kocDate, pageQuery);
    }
    /**
     * 查询我的全部可使用的产品流水
     */
    @ApiOperation("查询我的全部可使用的产品流水")
    @GetMapping("/queryCanUserMyProductLog")
    public R<List<TbUserKocProductLog>> queryCanUserMyProductLog() {
        return R.ok(tbUserKocProductLogService.list(new LambdaQueryWrapper<>(TbUserKocProductLog.class)
            .eq(TbUserKocProductLog::getUserId, getUserId()).eq(TbUserKocProductLog::getStatus, 0)));
    }
    @ApiOperation("查询我的全部可使用的koc任务兑换卷")
    @GetMapping("/queryExchangeCoupon")
    public R<List<TbProductExchangeCoupon>> queryExchangeCoupon() {
        return R.ok(tbProductExchangeCouponService.list(new LambdaQueryWrapper<>(TbProductExchangeCoupon.class)
            .eq(TbProductExchangeCoupon::getUserId, getUserId()).eq(TbProductExchangeCoupon::getStatus, CouponStatusEnum.UNUSED.getCode())));
    }
}
